﻿using System.Data.Common;
using System.Data;

namespace Tilver.Data
{
    /// <summary>
    /// DbCommand对象包装类
    /// 请通过DbDataBase.CreateCommand创建此对象
    /// </summary>
    public class CommandWrapper
    {
        private DbCommand _command;
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectionString { get; internal set; }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="command">包装的DbCommand对象</param>
        /// <param name="connectionString">连接字符串</param>
        internal CommandWrapper(DbCommand command, string connectionString)
        {
            _command = command;
            ConnectionString = connectionString;
        }

        /// <summary>
        /// 包装的DbCommand对象
        /// </summary>
        internal DbCommand Command { get { return _command; } }
        /// <summary>
        /// DbConnection对象
        /// </summary>
        internal DbConnection Connection { get { return _command.Connection; } set { _command.Connection = value; } }
        /// <summary>
        /// 数据库事务
        /// </summary>
        internal DbTransaction Transaction { get { return _command.Transaction; } set { _command.Transaction = value; } }
        /// <summary>
        /// 获取DbCommand对象的参数集合
        /// </summary>
        public DbParameterCollection Parameters { get { return _command.Parameters; } }
        /// <summary>
        /// 获取或设置DbCommand对象的CommandText属性
        /// </summary>
        public string CommandText { get { return _command.CommandText; } set { _command.CommandText = value; } }
        /// <summary>
        /// 获取或设置DbCommand对象的Timeout时间
        /// </summary>
        public int CommandTimeout { get { return _command.CommandTimeout; } set { _command.CommandTimeout = value; } }
        /// <summary>
        /// 获取或设置DbCommand对象的CommandType属性
        /// </summary>
        public CommandType CommandType { get { return _command.CommandType; } set { _command.CommandType = value; } }

        /// <summary>
        /// 执行查询语句并返回首行首列数据
        /// </summary>
        /// <returns></returns>
        public object ExecuteScalar()
        {
            return _command.ExecuteScalar();
        }
        /// <summary>
        /// 执行SQL语句并返回受影响的行数
        /// </summary>
        /// <returns></returns>
        public int ExecuteNonQuery()
        {
            return _command.ExecuteNonQuery();
        }
        /// <summary>
        /// 执行SQL语句并返回结果集
        /// </summary>
        /// <returns></returns>
        public DbDataReader ExecuteReader(CommandBehavior behavior = CommandBehavior.Default)
        {
            return _command.ExecuteReader(behavior);
        }

        public void Dispose()
        {
            _command.Dispose();
        }
    }
}
